import random


def fake_imei():
    # 随机生成86开头的14位数
    inital_num = str(8600000 + random.randint(1, 90000)) + str(1000000 + random.randint(1, 9000000))
    ch = list(inital_num)
    odd, even = 0, 0
    for i in range(len(ch)):
        tt = int(ch[i])
        if i % 2 == 0:
            odd += tt
        else:
            temp = tt * 2
            even += temp if temp < 10 else temp - 9

    # 得到IMEI的最后一位验证码：与10取余, 余数若为0，则验证位数字为0，若余数不为0，则验证位为(10-余数)
    remainder = int((odd + even) % 10)
    valid_num = 0 if remainder == 0 else 10 - remainder
    return inital_num + str(valid_num)


def check_mei(imei):
    # 把IMEI转化成list
    ch = list(imei)
    odd, even = 0, 0
    for i in range(len(ch) - 1):
        tt = int(ch[i])
        if i % 2 == 0:
            odd += tt
        else:
            temp = tt * 2
            even += temp if temp < 10 else temp - 9

    # 得到传入的IMEI的正确验证位并判断
    remainder = int((odd + even) % 10)
    valid_num = 0 if remainder == 0 else 10 - remainder
    return str(valid_num) == imei[-1]


if __name__ == '__main__':
    # 获取IMEI
    imei = fake_imei()
    print(imei)

    # 验证IMEI
    print(check_mei(imei))
